package com.geese.module.wechat.controller;

import com.geese.common.BaseController;
import com.geese.common.entity.Courier;
import com.geese.common.entity.WithdrawAccount;
import com.geese.common.util.DateUtil;
import com.geese.common.util.TextValidator;
import com.geese.common.vo.Page;
import com.geese.common.vo.PageVO;
import com.geese.exception.BusinessException;
import com.geese.module.user.model.SecurityUser;
import com.geese.module.user.service.UserService;
import com.geese.module.wechat.dto.WithdrawAccountDTO;
import com.geese.module.wechat.service.IWaiterService;
import com.geese.module.wechat.vo.CourierCashVO;
import com.geese.module.wechat.vo.CourierOrderVO;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 微信端小二控制器
 * Created by huweiwei on 2017/3/9.
 */
@RestController
@RequestMapping("/wechat/waiter")
@Scope("prototype")
public class WaiterController extends BaseController{

    private final static Logger logger = Logger.getLogger(WaiterController.class);

    @Autowired
    private IWaiterService waiterService;
    @Autowired
    private UserService userService;


    /**
     * 注册接口
     * @param courier 小二对象接收数据
     * @param password 密码
     * @return
     */
    @PostMapping("/register")
    public String register(Courier courier, @RequestParam String password) {

        if (StringUtils.isBlank(password)) {
            throw new BusinessException(120060);
        } else if (StringUtils.isBlank(courier.getName())) {
            throw new BusinessException(120013);
        } else if (StringUtils.isBlank(courier.getMobile()) || !TextValidator.isMobileExact(courier.getMobile())) {
            throw new BusinessException(120012);
        } else if (StringUtils.isBlank(courier.getStudentCard())) {
            throw new BusinessException(120062);
        } else if (StringUtils.isBlank(courier.getOpenId())) {
            throw new BusinessException(120061);
        } else if (null == courier.getSchoolId() || !(courier.getSchoolId() > 0)) {
            throw new BusinessException(120015);
        } else if (null == courier.getCampusId() || !(courier.getCampusId() > 0)) {
            throw new BusinessException(120039);
        } else if (null == courier.getBuildingId() || !(courier.getBuildingId() > 0)) {
            throw new BusinessException(120038);
        }

        waiterService.doRegister(courier, password);
        //不抛异常即注册成功
        return response();
    }

    /**
     * 根据订单id显示接单订单详情
     * @param orderId 订单主键
     * @return
     */
    @GetMapping("/showorder")
    public String showOrder(@RequestParam int orderId, @RequestParam String token) {
        userService.checkUser(token);
        if (orderId <= 0) {
            throw new BusinessException(120058);
        }
        Map<String, Object> result = waiterService.showOrder(orderId);
        return response(result);
    }

    /**
     * 根据订单code显示接单订单详情
     * @param orderCode 订单主键
     * @return
     */
    @GetMapping("/ordershow")
    public String orderShow(@RequestParam int orderCode, @RequestParam String token) {
        userService.checkUser(token);
        if (orderCode <= 0) {
            throw new BusinessException(120058);
        }
        Map<String, Object> result = waiterService.orderShow(orderCode);
        return response(result);
    }

    /**
     * 接单
     * @param orderId 订单主键
     * @return
     */
    @PostMapping("/orders")
    public String orders(@RequestParam int orderId, @RequestParam String token) {
        if (orderId <= 0) {
            throw new BusinessException(120058);
        }
        SecurityUser loginUser = userService.getLoginUser(token);
        waiterService.doOrders(orderId, loginUser);
        return response();
    }

    /**
     * 小二个人中心
     * @return
     */
    @GetMapping("/info")
    public String info(@RequestParam String token) {
        SecurityUser loginUser = userService.getLoginUser(token);
        Map<String, Object> result = waiterService.courierInfo(loginUser);
        return response(result);
    }

    /**
     * 查询小二账户余额和学校提现基数
     * @param token
     * @return
     */
    @GetMapping("/amount")
    public String amount(@RequestParam String token){
        SecurityUser loginUser = userService.getLoginUser(token);
        Map<String, Object> result = waiterService.amount(loginUser);
        return response(result);
    }

    /**
     * 小二订单列表页
     * @param startDate 起始时间
     * @param endDate   终止时间
     * @param currPage  查询页码
     * @param pageSize  页面大小
     * @return
     */
    @GetMapping("/list")
    public String list(@RequestParam (required = false) String startDate,
                       @RequestParam (required = false) String endDate,
                       @RequestParam (required = false) Integer currPage,
                       @RequestParam (required = false) Integer pageSize,
                       @RequestParam String token
    ) {
        SecurityUser loginUser = userService.getLoginUser(token);
        //封装查询参数
        CourierOrderVO courierOrderVO = new CourierOrderVO();
        courierOrderVO.setCurrPage(null == currPage || currPage <= 0 ? 1 : currPage);
        courierOrderVO.setPageSize(null == pageSize || pageSize <= 0 ? 15 : pageSize);
        if (startDate != null) {
            courierOrderVO.setStartDate(DateUtil.parseDate(startDate, DateUtil.PATTERN_ISO_ON_DATE));
        }
        if (endDate != null) {
            Date tempDate = DateUtil.parseDate(endDate, DateUtil.PATTERN_ISO_ON_DATE);
            courierOrderVO.setEndDate(DateUtil.addDays(tempDate, 1));
        }
        Map<String, Object> result = waiterService.orderList(courierOrderVO, loginUser);
        return response(result);
    }

    /**
     * 订单详情
     * @param orderId
     * @return
     */
    @GetMapping("/details")
    public String details(@RequestParam int orderId,
                          @RequestParam String token
    ) {
        userService.checkUser(token);
        if (orderId <= 0) {
            throw new BusinessException(120058);
        }
        Map<String, Object> result = waiterService.orderDetails(orderId);
        return response(result);
    }

    /**
     * 提现申请
     * @param token
     * @return
     */
    @GetMapping("/cash")
    public String cash(@RequestParam String token, @RequestParam int id) {
        if (id <= 0) {
            throw new BusinessException(120009);
        }
        SecurityUser user = userService.getLoginUser(token);
        waiterService.doCash(user, id);
        return response();
    }

    /**
     * 完成订单
     * @param token
     * @param orderId
     * @return
     */
    @GetMapping("/complete")
    public String complete(@RequestParam String token, @RequestParam int orderId) {
        if (orderId <= 0) {
            throw new BusinessException(120058);
        }
        SecurityUser user = userService.getLoginUser(token);
        waiterService.doComplete(user, orderId);
        return response();
    }

    /**
     * 添加提现账号
     * @param token
     * @param withdrawAccount
     * @return
     */
    @PostMapping("/addAccount")
    public String addAccount(@RequestParam String token, @RequestBody WithdrawAccount withdrawAccount){
        if (StringUtils.isBlank(withdrawAccount.getAccount())) {
            throw new BusinessException(120123);
        }
        if (withdrawAccount.getType() == null || (withdrawAccount.getType() != 1 && withdrawAccount.getType() != 0)) {
            throw new BusinessException(120124);
        }
        SecurityUser user = userService.getLoginUser(token);
        withdrawAccount.setUserId(user.getUserId());
        withdrawAccount.setEnable(1);
        withdrawAccount.setCreateTime(new Date());
        waiterService.addAccount(withdrawAccount);
        return response();
    }

    /**
     * 修改提现账号
     * @param token
     * @param withdrawAccount
     * @return
     */
    @PostMapping("/editAccount")
    public String editAccount(@RequestParam String token, @RequestBody WithdrawAccount withdrawAccount){
        if (withdrawAccount.getId() == null || withdrawAccount.getId() <= 0) {
            throw new BusinessException(120009);
        }
        SecurityUser user = userService.getLoginUser(token);
        withdrawAccount.setUserId(user.getUserId());
        waiterService.editAccount(withdrawAccount);
        return response();
    }

    /**
     * 删除提现账号
     * @param token
     * @param id
     * @return
     */
    @PostMapping("/deleteAccount")
    public String deleteAccount(@RequestParam String token, @RequestParam int id){
        if (id <= 0) {
            throw new BusinessException(120009);
        }
        waiterService.deleteAccount(id);
        return response();
    }

    /**
     * 获取提现账号列表
     * @param token
     * @return
     */
    @GetMapping("/listAccount")
    public String listAccount(@RequestParam String token){
        SecurityUser user = userService.getLoginUser(token);
        List<WithdrawAccountDTO> data = waiterService.listAccount(user.getUserId());
        Map<String, Object> result = Maps.newHashMap();
        result.put("accountList", data);
        return response(result);
    }

    /**
     * 提现申请列表
     * @param token
     * @param courierCashVO
     * @return
     */
    @GetMapping("/cashList")
    public String cashList(@RequestParam String token, CourierCashVO courierCashVO){
        SecurityUser user = userService.getLoginUser(token);
        Page page = getPageInfo(courierCashVO);
        Map<String, Object> data = waiterService.getCashList(user, page);
        return response(data);
    }
}
